package com.kayoxu.ble_printer.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.tbruyelle.rxpermissions2.RxPermissions;

/**
 * TFBlueAndroidXNew
 * null.java
 * <p>
 * Created by kayoxu on 12/9/20 at 9:48 AM
 * Copyright © 2020 kayoxu. All rights reserved.
 */

public class PermissionsUtils {
    
    public interface PermissionsCallback {

        void onAccept();
  
        void onRefused();

    }


    @SuppressLint("CheckResult")
    public static void request(Activity activity, PermissionsCallback callback, String... permission) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions.request(permission)
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        //表示用户同意权限
//                        Toast.makeText(activity,
//                                "用户同意使用权限", Toast.LENGTH_SHORT).show();
                        if (null != callback) callback.onAccept();
                    } else {
                        //表示用户不同意权限
//                        Toast.makeText(activity,
//                                "用户拒绝使用权限", Toast.LENGTH_SHORT).show();
                        if (null != callback) callback.onRefused();
                    }
                });
    }

    public static boolean Permission(Activity activity, String permission) {
        return ActivityCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
    }


}
